package jgl.vajra.operator.part.order.activity;

import android.text.TextUtils;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.MachineGuyBean;
import jgl.vajra.operator.databinding.ActivityMachineGuyBinding;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.manager.IntentManager;
import jgl.vajra.operator.part.order.contract.MachineGuyContract;
import jgl.vajra.operator.part.order.viewModel.MachineGuyViewModel;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(MachineGuyViewModel.class)
public class MachineGuyActivity extends BaseMVVMActivity<MachineGuyViewModel, ActivityMachineGuyBinding> implements MachineGuyContract.View, BaseBindingItemPresenter<MachineGuyBean> {

    public String logo;
    public String invitation;
    public int authorize=0;
    private SingleTypeBindingAdapter adapter;
    private List<MachineGuyBean> data;

    @Override
    public int getLayoutId() {
        return R.layout.activity_machine_guy;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        logo=getIntent().getStringExtra("logo");

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_machine_guy);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        mViewModel.getMachineGuyData(map);
    }

    //是否自主接单
    @Override
    public void onOrderReceiving() {
        if (authorize==0){
            authorize=1;
            mBinding.ivSwitch.setImageResource(R.drawable.ic_setting_open);
        }else {
            authorize=0;
            mBinding.ivSwitch.setImageResource(R.drawable.ic_setting_shut);
        }
    }

    //确定完成
    @Override
    public void onDetermine() {

        if(TextUtils.isEmpty(invitation)){
            ToastUtils.showShort("请选择默认机佬");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("logo", logo);
        map.put("invitation", invitation);
        map.put("authorize", authorize);
        mViewModel.selectDefaultDriver(map);

    }

    @Override
    public void returnSelectDefaultDriver(String data) {
        IntentManager.toSubmitSuccessActivity(this);
    }

    @Override
    public void returnMachineGuyData(List<MachineGuyBean> data) {
        this.data = data;
        adapter.refresh(data);
    }

    @Override
    public void onItemClick(int position, MachineGuyBean itemData) {
        if (itemData.isSwitch)
            return;

        for (int i = 0; i < data.size(); i++) {
            data.get(i).isSwitch=false;
        }
        itemData.isSwitch=true;
        adapter.refresh();
        invitation=itemData.invitation;
    }
}
